package com.cennavi.minemapexdemo.activity.mixlayer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.base.FunctionOptions;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.geometry.CoordinateConverter;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.FillLayer;
import com.minedata.minemap.overlay.FillLayerOptions;
import com.minedata.minemap.overlay.ImageLayer;
import com.minedata.minemap.overlay.ImageOptions;
import com.minedata.minemap.overlay.MarkerLayer;
import com.minedata.minemap.overlay.MarkerLayerOptions;
import com.minedata.minemap.overlay.PolyLineLayer;
import com.minedata.minemap.overlay.PolyLineLayerOptions;
import com.cennavi.minemapexdemo.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//混合wmts图像图demo
public class MixWmtsActivity extends AppCompatActivity {
    private MapView mapView;
    private static MineMap mineMap;
    private ImageLayer imageLayer;
    private MarkerLayer markerLayer;
    private PolyLineLayer lineLayer;
    private FillLayer fillLayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MinemapAccountManager.getInstance(getApplicationContext(),
                "57d9ee4eec2d4b1f87f8efa7eba2178e",
                null,
                "5696");


        setContentView(R.layout.activity_mix_wmts);
        mapView = findViewById(R.id.mapView);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(final MineMap mineMap) {
                MixWmtsActivity.mineMap = mineMap;
                mineMap.setWmtsEnabled(true);
                mineMap.setStyleUrl("http://minedata.cn/service/solu/style/id/5696", new MineMap.OnStyleLoadedListener() {

                    @Override
                    public void onStyleLoaded(String s) {
                        addImageGraphics();
                    }
                });
                mineMap.setRepaint(true);
                //不显示指南针
                mineMap.getUiSettings().setCompassEnabled(false);
                //不可以旋转地图
                mineMap.getUiSettings().setRotateGesturesEnabled(false);
                mineMap.setCameraPosition(
                        new CameraPosition.Builder()
                                .target(new LatLng(32.05838, 118.94647))//设置相机指向的位置
                                .zoom(9)//设置相机缩放等级
                                .tilt(0)//设置相机的俯视角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .build());
                //设置是否显示地图调试信息，默认是不显示
                mineMap.setDebugActive(false);
            }
        });
    }

    void addImageGraphics() {
        ImageOptions options = new ImageOptions("raster", "http://218.2.231.245/mapjs2/rest/services/MapJS/js_yxdt_2016/MapServer/tile/{z}/{y}/{x}");
        mineMap.addLayer(options);
        ImageOptions options1 = new ImageOptions("raster1", "http://218.2.231.245/mapjs2/rest/services/MapJS/js_yxzj_2016/MapServer/tile/{z}/{y}/{x}");
        imageLayer = mineMap.addLayer(options1);
    }

    @Override
    protected void onStart() {
        if (mapView != null) {
            mapView.onStart();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {
        if (mapView != null)
            mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mapView != null)
            mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (mapView != null)
            mapView.onStop();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        if (mapView != null)
            mapView.onDestroy();
        super.onDestroy();
    }

    public void btnMarkerlayer(View view) {
        LatLng latLng = new LatLng(32.0703, 118.7943);//玄武湖景区
        CoordinateConverter converter=new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GCJ02);
        converter.coord(latLng);
        latLng = converter.convert();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                android.R.mipmap.sym_def_app_icon);
        String bitmapName = "sym_def_app_icon";
        mineMap.addImage(bitmapName, bitmap);
        List<MarkerLayerOptions.MarkerItem> symbolItems = new ArrayList<>();
        symbolItems.add(new MarkerLayerOptions.MarkerItem()
                .icon(bitmapName).position(latLng).iconRotate(30.0f));
        MarkerLayerOptions options = new MarkerLayerOptions("iconmarkerlayer")
                .iconPitchAlignment(MarkerLayerOptions.PITCH_ALIGNMENT_MAP)
                .iconRotationAlignment(MarkerLayerOptions.ROTATION_ALIGNMENT_VIEWPORT);
        options.addAll(symbolItems);
        markerLayer = mineMap.addLayer(options);
    }

    public void btnPolyLinelayer(View view) {
        List<PolyLineLayerOptions.PolylineItem> items = new ArrayList<>();
        LatLng latLng1=new LatLng(32.0582, 118.7965);//南京市政府
        LatLng latLng2=new LatLng(32.0555, 118.7973);//南京市中心医院
        LatLng latLng3=new LatLng(32.04845, 118.7980);//玄武区政府
        LatLng latLng4=new LatLng(32.03156, 118.7942);//郑和公园
        CoordinateConverter converter=new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GCJ02);
        converter.coord(latLng1);
        latLng1 = converter.convert();
        converter.coord(latLng2);
        latLng2 = converter.convert();
        converter.coord(latLng3);
        latLng3 = converter.convert();
        converter.coord(latLng4);
        latLng4 = converter.convert();

        List<LatLng> linePoints = new ArrayList<LatLng>();
        linePoints.add(latLng1);
        linePoints.add(latLng2);
        linePoints.add(latLng3);
        linePoints.add(latLng4);

        items.add(new PolyLineLayerOptions.PolylineItem(linePoints, "#ffff0000"));

        PolyLineLayerOptions polyLineLayerOptions = new PolyLineLayerOptions("line1")
                .lineWidth(4.0f)
                .alpha(0.9f);

        polyLineLayerOptions.addAll(items);
        polyLineLayerOptions.setVisible(FunctionOptions.VISIBLE);

        lineLayer = mineMap.addLayer(polyLineLayerOptions);
    }

    public void btnFillLayer(View view) {
        LatLng latLng1=new LatLng(32.0599, 118.8525);//中山陵
        LatLng latLng2=new LatLng(31.9683, 118.7998);//月牙湖公园
        LatLng latLng3=new LatLng(31.9579, 118.8897);//上坊镇

        CoordinateConverter converter=new CoordinateConverter();
        converter.from(CoordinateConverter.CoordType.GCJ02);
        converter.coord(latLng1);
        latLng1 = converter.convert();
        converter.coord(latLng2);
        latLng2 = converter.convert();
        converter.coord(latLng3);
        latLng3 = converter.convert();

        List<LatLng> latLngs = new ArrayList<>();
        latLngs.add(latLng1);
        latLngs.add(latLng2);
        latLngs.add(latLng3);

        List<FillLayerOptions.FillItem> items = new ArrayList<>();
        FillLayerOptions.FillItem item1 = new FillLayerOptions.FillItem(latLngs);
        item1.setAlpha(0.8f);
        item1.setColor(Color.parseColor("#EE82EE"));
        item1.setOutlineColor(Color.parseColor("#FF00FF"));
        items.add(item1);
        FillLayerOptions options = new FillLayerOptions("filllayer");
        options.addAll(items);
        fillLayer = mineMap.addLayer(options);
    }

    public void btnTest(View view) {
        mineMap.snapshot(new MineMap.OnSnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap snapshot) {
                //图片存放路径
                String sdCardDir = "/storage/emulated/0/Android/data/com.minedata.minemap/files/";

                try {
                    File dirFile = new File(sdCardDir);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    //创建日期
                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    String sdfDate = sdf.format(date);
                    File file = new File(sdCardDir, "snapshot" + "(" + sdfDate + ")" + ".png");
                    FileOutputStream fos = new FileOutputStream(file);
                    snapshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
